package com.lkwlw.tplms.system.action;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.lkwlw.tplms.mapper.DriverInfoMapper;
import com.lkwlw.tplms.mapper.TransShipmentStationMapper;
import com.lkwlw.tplms.model.DriverInfo;
import com.lkwlw.tplms.repertory.dao.mapper.WarehouseinfoMapper;
import com.lkwlw.tplms.repertory.dao.model.Warehouseinfo;
import com.lkwlw.tplms.system.dao.mapper.AdmininfoMapper;
import com.lkwlw.tplms.system.dao.mapper.UserAuthorityMapper;
import com.lkwlw.tplms.system.dao.model.Admininfo;
import com.lkwlw.tplms.system.dao.model.AuthorityExample;
import com.lkwlw.tplms.system.dao.model.UserAuthority;
import com.lkwlw.tplms.system.dao.model.UserAuthorityExample;
import com.lkwlw.tplms.system.dao.model.UserAuthorityExample.Criteria;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UserAuthorityAction extends ActionSupport {
	private static final long serialVersionUID = 8843295033506954971L;
	
	private String roles;
	
	private String repertoryNo;
	
	private String repertoryName;
	
	private String repertoryAddre;
	
	private String repertoryType;
	
	private String[] authoritys;
	
	private Integer userId;
	
	private WarehouseinfoMapper warehouseinfoMapper;
	
	private UserAuthorityMapper userAuthorityMapper;
	
	private DriverInfoMapper driverInfoMapper;
	
	private AdmininfoMapper adminInfoMapper;
	
	private String vehicleno;
	
	private String stationId;
	
	private TransShipmentStationMapper transShipStatMapper;
	
	public String execute(){
		Admininfo user = adminInfoMapper.selectByPrimaryKey(userId);
		 if(roles.equals("3")){
			 Warehouseinfo ware = new Warehouseinfo();
			ware.setRegisterdate(new Date());
			ware.setUserId(userId);
			ware.setWhkeeperid(userId);
			ware.setWarehouse(repertoryName);
			ware.setWaretype(repertoryType);
			ware.setWhlocation(repertoryAddre);
			ware.setId(repertoryNo);
			ware.setRegistrant("asd");
			ActionContext context = ActionContext.getContext();
			Map session = context.getSession();
			Admininfo admin = (Admininfo) session.get("admin");
			if (null != admin) {
				ware.setRegistrant(admin.getFullName());
			}
			//Admininfo user = adminInfoMapper.selectByPrimaryKey(userId);
			ware.setWhkeeper(user.getFullName());
			warehouseinfoMapper.insertSelective(ware);			 
		 }
		if(null != authoritys && 0 < authoritys.length){
			UserAuthorityExample example = new UserAuthorityExample();
			Criteria criteria = example.createCriteria();
			criteria.andUserIdEqualTo(userId); // 更新用户权限表前先删除
			userAuthorityMapper.deleteByExample(example);
			for (int i = 0; i < authoritys.length; i++) {
				UserAuthority userAuthority = new UserAuthority();
				userAuthority.setAuthorityCode(authoritys[i]);
				userAuthority.setRole(roles);
				userAuthority.setUserId(userId);
				
				userAuthorityMapper.insertSelective(userAuthority);
			}
		}
		if (roles.equals("5")) {
			//Admininfo user = adminInfoMapper.selectByPrimaryKey(userId);
			DriverInfo driver = driverInfoMapper.selectByPrimaryKey(user.getCardNo());
			driver.setVehicleNo(vehicleno);
			Map paramMap = new HashMap();
			paramMap.put("vehicleNo", vehicleno);
			paramMap.put("idCardNo", user.getCardNo());
			driverInfoMapper.updateDriverVehicleNoByPrimaryKey(paramMap);
			//driver = driverInfoMapper.selectByPrimaryKey(user.getCardNo());
		}
		if (roles.equals("7")) {
			if (null != transShipStatMapper.selectByPrimaryKey(stationId)) {
				Map paramMap = new HashMap();
				paramMap.put("stationId", stationId);
				paramMap.put("stationAdmin", user.getFullName());
				paramMap.put("stationAdminId", user.getId());
				transShipStatMapper.updateStationAdminByPrimaryKey(paramMap);
			}			
		}
		return SUCCESS;
	}

	public void setRoles(String roles) {
		this.roles = roles;
	}

	public void setRepertoryNo(String repertoryNo) {
		this.repertoryNo = repertoryNo;
	}

	public void setRepertoryName(String repertoryName) {
		this.repertoryName = repertoryName;
	}

	public void setRepertoryAddre(String repertoryAddre) {
		this.repertoryAddre = repertoryAddre;
	}

	public void setRepertoryType(String repertoryType) {
		this.repertoryType = repertoryType;
	}

	public void setAuthoritys(String[] authoritys) {
		this.authoritys = authoritys;
	}

	public void setUserId(Integer userId) {
		this.userId = userId;
	}

	public void setWarehouseinfoMapper(WarehouseinfoMapper warehouseinfoMapper) {
		this.warehouseinfoMapper = warehouseinfoMapper;
	}

	public void setUserAuthorityMapper(UserAuthorityMapper userAuthorityMapper) {
		this.userAuthorityMapper = userAuthorityMapper;
	}

	public String getVehicleno() {
		return vehicleno;
	}

	public void setVehicleno(String vehicleno) {
		this.vehicleno = vehicleno;
	}

	public void setDriverInfoMapper(DriverInfoMapper driverInfoMapper) {
		this.driverInfoMapper = driverInfoMapper;
	}

	public void setAdminInfoMapper(AdmininfoMapper adminInfoMapper) {
		this.adminInfoMapper = adminInfoMapper;
	}
	

	public String getStationId() {
		return stationId;
	}

	public void setStationId(String stationId) {
		this.stationId = stationId;
	}	

	public void setTransShipStatMapper(
			TransShipmentStationMapper transShipStatMapper) {
		this.transShipStatMapper = transShipStatMapper;
	}
}
